//
//  LoginNextStepViewController.m
//  didacar
//
//  Created by imac on 15/12/5.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "LoginNextStepViewController.h"
#import "JKCountDownButton.h"
#import "UIImageView+WebCache.h"
#import "UserViewModel.h"
#import "SmsCodeModel.h"
#import "SVProgressHUD.h"
#import "CommonAddressViewModel.h"

@interface LoginNextStepViewController ()

@property (strong, nonatomic) IBOutlet UILabel *tipLabel;
@property (strong, nonatomic) IBOutlet UITextField *textField;

@property (strong, nonatomic) IBOutlet JKCountDownButton *reGetCodeBtn;
- (IBAction)countDownAction:(JKCountDownButton *)sender;

- (IBAction)loginAction:(UIButton *)sender;

@property (strong, nonatomic) IBOutlet UIView *contentView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *layout_top;

@property (strong, nonatomic) UserViewModel *userViewModel;


@property (weak, nonatomic) IBOutlet UIImageView *bannerImg;

@end

@implementation LoginNextStepViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CommoneTools changeConstraintView:self.bannerImg constraintHeight:GTFixHeightFloat(137)];
    
    
    self.userViewModel = [[UserViewModel alloc ] init];
    [self setLeftBtnItem];
    
    self.textField.leftView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.layer.borderColor = GLOBLE_GRAY_COLOR_1.CGColor;
    self.textField.layer.cornerRadius = 6;
    self.textField.layer.masksToBounds= YES;
    self.textField.layer.borderWidth = 0.45;
    
    self.reGetCodeBtn.layer.cornerRadius = 4;
    self.reGetCodeBtn.layer.masksToBounds = YES;
    
    self.tipLabel.text = [NSString stringWithFormat:@"验证码已发送至 : %@",self.mobile];
    [self countDown];
    
  
}

-(void)viewDidAppear:(BOOL)animated{
    [self.textField becomeFirstResponder];
    [super viewDidAppear:animated];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (MOBILEMODEL_4S) {
        WEAKSELF
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.layout_top.constant = - 50;
            [weakSelf.contentView layoutIfNeeded];
        }];
    }
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    if (MOBILEMODEL_4S) {
        WEAKSELF
        [UIView animateWithDuration:0.2 animations:^{
            weakSelf.layout_top.constant = 0;
            [weakSelf.contentView layoutIfNeeded];
        }];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (IBAction)countDownAction:(JKCountDownButton *)sender {
    [self sendSmsCode];
}

-(void)countDown{
    self.reGetCodeBtn.enabled = NO;
    [self.reGetCodeBtn startWithSecond:60];
    
    [self.reGetCodeBtn didChange:^NSString *(JKCountDownButton *countDownButton,int second) {
        NSString *title = [NSString stringWithFormat:@"%d秒后重新获取",second];
        self.reGetCodeBtn.backgroundColor = UI_RGBA(207, 207, 207, 1);
        return title;
    }];
    
    [self.reGetCodeBtn didFinished:^NSString *(JKCountDownButton *countDownButton, int second) {
        countDownButton.enabled = YES;
        self.reGetCodeBtn.backgroundColor = GLOBLE_ORANGE_COLOR;
        return @"点击重新获取";
    }];
}

// 发送验证码
-(void)sendSmsCode{
    self.userViewModel.mobile = self.mobile;
    WEAKSELF
    [self.userViewModel sendSmsCode:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [weakSelf countDown];
            
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
    }];
}

- (IBAction)loginAction:(UIButton *)sender {
    [self.textField resignFirstResponder];
        self.userViewModel.mobile = self.mobile;
        self.userViewModel.smsCode = self.textField.text;
        [self.userViewModel login:^(NSInteger resultType, id data, NSString *errorMsg) {
            if (resultType == 1) {
                
                
                [CommonAddressViewModel getMyAddress:^(NSInteger resultType, id data, NSString *errorMsg) {
                    if (resultType == 1) {
                        [self dismissViewControllerAnimated:YES completion:^{
                            
                        }];
                    }else{
                         [SVProgressHUD showErrorWithStatus:errorMsg];
                    }
                    
                }];
                
                
               
            }else{
                [SVProgressHUD showErrorWithStatus:errorMsg];
            }
        }];
}

@end
